<?php

namespace catchAdmin\WebSocket\controller;

use GatewayWorker\Lib\Gateway;
use think\facade\Log;
use Workerman\Worker;

class Ws
{

    /**
     * onConnect 事件回调
     * 当客户端连接上gateway进程时(TCP三次握手完毕时)触发
     *
     * @access public
     * @param int $client_id
     * @return void
     */
    public static function onConnect($client_id)
    {

//         向当前client_id发送数据
         Gateway::sendToClient($client_id, "Hello $client_id\r\n");
//         向所有人发送
         Gateway::sendToAll("$client_id login\r\n");

        Gateway::sendToCurrentClient("Your client_id is $client_id");
    }


    /**
     * onMessage 事件回调
     * 当客户端发来数据(Gateway进程收到数据)后触发
     *
     * @access public
     * @param int $client_id
     * @param mixed $data
     */
    public static function onMessage($client_id, $message)
    {
        $data = stripslashes(html_entity_decode($message));
        $p5 = json_decode($data, true);

        p($p5);
        Log::write($p5);

        if ($p5["type"] == 'bind') {

            Gateway::bindUid($client_id, $p5['info']);
        }

    }

}